import { useCallback, useState } from "react";
import { loadTodos } from "storage";
import { ITodo } from "types/todo";

// 封装搜索todo的自定义hooks
export const useSearchTodo = () => {
  const [todos, setSearchTodos] = useState<ITodo[]>([]);

  // 搜索todo
  const searchTodos = useCallback((keywords: string) => {
    const res = [];
    const nowtodos = loadTodos();
    for (let todo of nowtodos) {
      if (todo.content.indexOf(keywords.trim()) !== -1) {
        res.push(todo);
      }
    }
    console.log("搜索结果: ", res);
    setSearchTodos(res);
  }, []);

  return {
    todos,
    searchTodos,
  };
};
